package com.tc.dispatch.web.schedule.similary;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import com.tc.dispatch.gsservice.basic.common.tcdate;
import com.tc.dispatch.gsservice.ejb.GSServiceLocal;
import com.tc.dispatch.gsservice.exception.GSException;
import com.tc.dispatch.gsservice.model.OrderSumMain;
import com.tc.dispatch.web.gsservice.gsHelper;
import com.tc.dispatch.web.model.combinedDensity;
import com.tc.dispatch.web.model.combinedWeight;
import com.tc.dispatch.web.util.DateJsonValueProcessor;
import com.tc.dispatch.web.util.tools;

public class similaryWeight extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public similaryWeight() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html; charset=utf-8");
		// response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		String action = request.getParameter("action");
		if (action == null)
			action = "";

		GSServiceLocal gss = gsHelper.getGSServiceLocal();
		HttpSession sess = request.getSession();
		if (action.equals("list")) {

			try {

				HashMap retMap = gss.getWeight();
				List ret = new ArrayList();
				Iterator it = retMap.entrySet().iterator();
				while (it.hasNext()) {
					combinedWeight cd = new combinedWeight();
					Map.Entry me = (Map.Entry) it.next();
					int[] arr = (int[]) me.getValue();
					String name = (String) me.getKey();
					cd.setName(name);
					if (arr.length == 1) {

						cd.setAttribute1(arr[0]);
						cd.setAttribute2(0);
						cd.setAttribute3(0);
						cd.setAttribute4(0);
						cd.setAttribute5(0);
						cd.setAttribute6(0);
					} else if (arr.length == 2) {
						cd.setAttribute1(arr[0]);
						cd.setAttribute2(arr[1]);
						cd.setAttribute3(0);
						cd.setAttribute4(0);
						cd.setAttribute5(0);
						cd.setAttribute6(0);
					} else if (arr.length == 3) {
						cd.setAttribute1(arr[0]);
						cd.setAttribute2(arr[1]);
						cd.setAttribute3(arr[2]);
						cd.setAttribute4(0);
						cd.setAttribute5(0);
						cd.setAttribute6(0);
					} else if (arr.length == 4) {
						cd.setAttribute1(arr[0]);
						cd.setAttribute2(arr[1]);
						cd.setAttribute3(arr[2]);
						cd.setAttribute4(arr[3]);
						cd.setAttribute5(0);
						cd.setAttribute6(0);
					} else if (arr.length == 5) {
						cd.setAttribute1(arr[0]);
						cd.setAttribute2(arr[1]);
						cd.setAttribute3(arr[2]);
						cd.setAttribute4(arr[3]);
						cd.setAttribute5(arr[4]);
						cd.setAttribute6(0);
					} else if (arr.length == 6) {
						cd.setAttribute1(arr[0]);
						cd.setAttribute2(arr[1]);
						cd.setAttribute3(arr[2]);
						cd.setAttribute4(arr[3]);
						cd.setAttribute5(arr[4]);
						cd.setAttribute6(arr[5]);
					}

					ret.add(cd);
				}
				JSONObject json = new JSONObject();
				JsonConfig cfg = new JsonConfig();
				cfg.registerJsonValueProcessor(java.util.Date.class,
						new DateJsonValueProcessor());
				cfg.registerJsonValueProcessor(java.sql.Date.class,
						new DateJsonValueProcessor());

				// BeanUtils.copyProperties();
				json.put("totalProperty", new Integer(ret.size()));
				JSONArray arr = JSONArray.fromObject(ret.toArray(), cfg);
				json.put("root", arr);
				// sess.setAttribute("density", retMap);

				response.getWriter().print(json);
			} catch (Exception e) {

				System.out.println("list error:" + e.getMessage());
				response.getWriter().print("{success:false}");
			}

		} else if (action.equals("update")) {
			String name = request.getParameter("name");
			String attribute1 = request.getParameter("attribute1");
			String attribute2 = request.getParameter("attribute2");
			String attribute3 = request.getParameter("attribute3");
			String attribute4 = request.getParameter("attribute4");
			String attribute5 = request.getParameter("attribute5");
			String attribute6 = request.getParameter("attribute6");
			try {
				int[] val = new int[6];
				val[0] = Integer.parseInt(attribute1);
				val[1] = Integer.parseInt(attribute2);
				val[2] = Integer.parseInt(attribute3);
				val[3] = Integer.parseInt(attribute4);
				val[4] = Integer.parseInt(attribute5);
				val[5] = Integer.parseInt(attribute6);
				HashMap hs = gss.getWeight();
				Iterator it = hs.entrySet().iterator();
				while (it.hasNext()) {
					if (name.equals(((Map.Entry) it.next()).getKey())) {

						hs.put(name, val);
						break;
					}
				}
				gss.setWeight(hs);
			}

			catch (Exception e) {
				System.out.println("exception:" + e.getMessage());
			}
			out.print("{success:true, data:'sucess'}");
		}
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html; charset=utf-8");
		// response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		String action = request.getParameter("action");
		if (action == null)
			action = "";

		GSServiceLocal gss = gsHelper.getGSServiceLocal();
		HttpSession sess = request.getSession();
		if (action.equals("list")) {

			try {

				HashMap retMap = gss.getWeight();
				List ret = new ArrayList();
				Iterator it = retMap.entrySet().iterator();
				while (it.hasNext()) {
					combinedWeight cd = new combinedWeight();
					Map.Entry me = (Map.Entry) it.next();
					int[] arr = (int[]) me.getValue();
					String name = (String) me.getKey();
					cd.setName(name);
					if (arr.length == 1) {

						cd.setAttribute1(arr[0]);
						cd.setAttribute2(0);
						cd.setAttribute3(0);
						cd.setAttribute4(0);
						cd.setAttribute5(0);
						cd.setAttribute6(0);
					} else if (arr.length == 2) {
						cd.setAttribute1(arr[0]);
						cd.setAttribute2(arr[1]);
						cd.setAttribute3(0);
						cd.setAttribute4(0);
						cd.setAttribute5(0);
						cd.setAttribute6(0);
					} else if (arr.length == 3) {
						cd.setAttribute1(arr[0]);
						cd.setAttribute2(arr[1]);
						cd.setAttribute3(arr[2]);
						cd.setAttribute4(0);
						cd.setAttribute5(0);
						cd.setAttribute6(0);
					} else if (arr.length == 4) {
						cd.setAttribute1(arr[0]);
						cd.setAttribute2(arr[1]);
						cd.setAttribute3(arr[2]);
						cd.setAttribute4(arr[3]);
						cd.setAttribute5(0);
						cd.setAttribute6(0);
					} else if (arr.length == 5) {
						cd.setAttribute1(arr[0]);
						cd.setAttribute2(arr[1]);
						cd.setAttribute3(arr[2]);
						cd.setAttribute4(arr[3]);
						cd.setAttribute5(arr[4]);
						cd.setAttribute6(0);
					} else if (arr.length == 6) {
						cd.setAttribute1(arr[0]);
						cd.setAttribute2(arr[1]);
						cd.setAttribute3(arr[2]);
						cd.setAttribute4(arr[3]);
						cd.setAttribute5(arr[4]);
						cd.setAttribute6(arr[5]);
					}

					ret.add(cd);
				}
				JSONObject json = new JSONObject();
				JsonConfig cfg = new JsonConfig();
				cfg.registerJsonValueProcessor(java.util.Date.class,
						new DateJsonValueProcessor());
				cfg.registerJsonValueProcessor(java.sql.Date.class,
						new DateJsonValueProcessor());

				// BeanUtils.copyProperties();
				json.put("totalProperty", new Integer(ret.size()));
				JSONArray arr = JSONArray.fromObject(ret.toArray(), cfg);
				json.put("root", arr);
				// sess.setAttribute("density", retMap);

				response.getWriter().print(json);
			} catch (Exception e) {

				System.out.println("list error:" + e.getMessage());
				response.getWriter().print("{success:false}");
			}

		} else if (action.equals("update")) {
			String name = request.getParameter("name");
			String attribute1 = request.getParameter("attribute1");
			String attribute2 = request.getParameter("attribute2");
			String attribute3 = request.getParameter("attribute3");
			String attribute4 = request.getParameter("attribute4");
			String attribute5 = request.getParameter("attribute5");
			String attribute6 = request.getParameter("attribute6");
			try {
				int[] val = new int[6];
				val[0] = Integer.parseInt(attribute1);
				val[1] = Integer.parseInt(attribute2);
				val[2] = Integer.parseInt(attribute3);
				val[3] = Integer.parseInt(attribute4);
				val[4] = Integer.parseInt(attribute5);
				val[5] = Integer.parseInt(attribute6);
				HashMap hs = gss.getWeight();
				Iterator it = hs.entrySet().iterator();
				while (it.hasNext()) {
					if (name.equals(((Map.Entry) it.next()).getKey())) {

						hs.put(name, val);
						break;
					}
				}
				gss.setWeight(hs);
			}

			catch (Exception e) {
				System.out.println("exception:" + e.getMessage());
			}
			out.print("{success:true, data:'sucess'}");
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
